from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from apps.verifications.serializer import EmailSendserializer
from .models import User
from rest_framework.generics import GenericAPIView,CreateAPIView,RetrieveAPIView,UpdateAPIView
from rest_framework import mixins
from . import serializers
from rest_framework.permissions import IsAuthenticated

class CheckIsernameView(APIView):
    def get(self,request,username):

        #1.根据用户名查询,用户数量
        count = User.objects.filter(username=username).count

        #2.返回响应
        data ={
            "count":count
        }
        return Response(data,status=status.HTTP_200_OK)

class CheckMobileView(APIView):
        def get(self, request, mobile):
            # 1.根据手机号查询,用户数量
            count = User.objects.filter(mobile=mobile).count

            # 2.返回响应
            data = {
                "count": count
            }
            return Response(data, status=status.HTTP_200_OK)


class RegisterUserView(CreateAPIView):
    serializer_class = serializers.RegiserUserSerializer

    # def post(self,request):
        # #1.获取参数
        # dict_data = request.data
        # #创建序列化器.校验
        # serializer = self.get_serializer(data=dict_data)
        # serializer.is_valid(raise_exception=True)
        # # 3.数据入库
        # serializer.save()
        #
        # #4.返回响应
        # return Response(serializer.data,status=status.HTTP_201_CREATED)


        #二.使用了mixin + GenericsAPIView
        # return self.create(request)


class UserInfoView(RetrieveAPIView):
    serializer_class = serializers.UserInfoSerializer
    permission_classes = [IsAuthenticated]
    def get_object(self):
        return self.request.user
    # def get(self,request):
    #     #1.获取用户对象
    #     user = request.user
    #     self.get_object()
    #
    #
    #     #2创建序列化对象
    #     serializer = self.get_serializer(instance=user)
    #
    #     #3.返回数据
    #     return Response(serializer.data)


class EmailSendView(UpdateAPIView):
    serializer_class = EmailSendserializer
    permission_classes = [IsAuthenticated]
    def put(self,request):
        return self.request.user
    #     #1.获取数据
    #     dict_data = request.data
    #     user = self.get_object()
    #     #2.获取序列化器
    #     serializer = self.get_serializer(isinstance=user,data=dict_data)
    #
    #
    #
    #     #3.校验,入库
    #     serializer.is_valid(raise_exception=True)
    #     serializer.save()
    #     #4.返回
    #     return Response(serializer.data)